package com.chanyachting.emall.action.index;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.domain.Channel;
import com.chanyachting.emall.domain.Company;
import com.chanyachting.emall.domain.Rentyacht;
import com.chanyachting.emall.form.RentyachtForm;
import com.chanyachting.emall.service.ChannelService;
import com.chanyachting.emall.service.CompanyService;
import com.chanyachting.emall.service.RentyachtService;
import com.chanyachting.emall.serviceImpl.ChannelServiceImpl;
import com.chanyachting.emall.serviceImpl.CompanyServiceImpl;
import com.chanyachting.emall.serviceImpl.RentyachtServiceImpl;

public class QueryRentBySubChannelAction  extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		//主页页面记录数
		int COMMON_PAGE_SIZE = 6;
		
		RentyachtService ryService = new RentyachtServiceImpl();
		RentyachtForm rf = (RentyachtForm)form;
		ChannelService cService = new ChannelServiceImpl();
		CompanyService cpService = new CompanyServiceImpl();
		
		int currentPage = rf.getCurrentPage();
		int pageSize = COMMON_PAGE_SIZE;
		int channelId = rf.getChannelId();
		
		Channel c = cService.getOneChannel(new Channel(channelId));
		
		String channelName = rf.getChannelName();
		if (c == null) {
			return mapping.findForward("error");
		}
		channelName = c.getName();
		
		List<Channel> subChannelList = cService.getSubChannel(c);
		
		if (subChannelList == null || subChannelList.size() == 0) {
			return mapping.findForward("error");
		}
		
		String subChannelId = rf.getSubChannelId();
		String subChannelName = "";
		boolean isExistSub =false;
		
		for(Channel tmpC:subChannelList) {
			if (String.valueOf(tmpC.getId()).equals(subChannelId)) {
				subChannelName = tmpC.getName();
				isExistSub = true;
			}
		}
		
		if (!isExistSub) {
			return mapping.findForward("error");
		}
		
		int pageCount = ryService.QueryAllRentyachtCountByChannelId(subChannelId)/pageSize;
		if (pageCount%pageSize > 0) {
			pageCount = pageCount + 1;
		}
		
		if (currentPage < 1) {
			currentPage = 1;
		} else if (currentPage > pageCount) {
			currentPage = 1;
		}
		List<Rentyacht> ryList = ryService.QueryAllRentyacht(currentPage, pageSize,subChannelId);
		rf.setRyList(ryList);
		Map<String,Rentyacht> channelList = new TreeMap<String,Rentyacht>();
		
		for(int i=0;i<ryList.size();i++) {
			Company cp = new Company(ryList.get(i).getCompanyId());
			cp = cpService.getOneCompany(cp);
			String companyName = "";
			if (cp != null) {
				companyName = cp.getName();
			}
			channelList.put(companyName + "&" + i, ryList.get(i));
		}
		rf.setChannelList(channelList);
		rf.setSubChannelId(subChannelId);
		rf.setChannelName(channelName);
		rf.setSubChannelList(subChannelList);
		rf.setSubChannelName(subChannelName);
		
		return mapping.findForward("toAllRentyachtBySubChannelId");
	}
}
